<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    //冒泡排序
    let arr = [16, 64, 32, 9, 19, 21, 17];

    function bubble(arr) {
        for (let i = 0; i < arr.length; i++) {
            for (let j = 0; j < arr.length - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    let temp;
                    temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        return arr;
    }

    console.log(bubble(arr));

    //二分查找/折半查找
    function bin_search(n, array) {
        let flag;
        let begin = 0,
            end = arr.length - 1;
        while (begin <= end) {
            mid = parseInt((begin + end) / 2);
            if (n < array[mid]) {
                end = mid - 1;
            } else if (n > array[mid]) {
                begin = mid + 1;
            } else {
                flag = 1;
                break;
            }
        }
        if (begin > end) {
            flag = 0
        }
        return flag;
    }
    let a = [9, 16, 17, 19, 21, 32, 64];
    let num = parseInt(prompt('输入要找的数：'));
    if (bin_search(num, a)) {
        console.log('找到' + num + '了');
    } else {
        console.log('没找到！');
    }
</script>


</html>